package com.dylan.历史.领扣;

import java.util.Arrays;

/**
 * @Auther: ankang
 * @Date: 2019/3/4 21:25
 * @Description:
 */
public class $0067二进制求和 {

    public static void main(String[] args) {
        String result = new Solution().addBinary("1111101", "10");
        System.out.println(Arrays.toString(result.toCharArray()));
    }

    static class Solution {
        public String addBinary(String a, String b) {
            int aLen = a.length();
            int bLen = b.length();
            int cLen = (aLen > bLen ? aLen : bLen) + 1;
            char[] cArr = new char[cLen];
            boolean add = false; // 进位
            int offset = 0;
            char aOffsetChar;
            char bOffsetChar;
            while (++offset <= cLen) {
                aOffsetChar = '0';
                bOffsetChar = '0';
                if (offset <= aLen) {
                    aOffsetChar = a.charAt(aLen - offset);
                }
                if (offset <= bLen) {
                    bOffsetChar = b.charAt(bLen - offset);
                }
                if (aOffsetChar == bOffsetChar) {
                    cArr[cLen - offset] = add ? '1' : '0';
                    add = aOffsetChar == '1';
                } else {
                    cArr[cLen - offset] = add ? '0' : '1';
                }
            }
            if (cArr[0] == '1') {
                return new String(cArr);
            } else {
                char[] cArr2 =  new char[cLen - 1];
                System.arraycopy(cArr, 1, cArr2, 0, cLen - 1);
                return new String(cArr2);
            }
        }
    }
}
